package cn.wecloud.im.multiplayervideodemo.http

import cn.wecloud.im.multiplayervideodemo.http.model.BaseHttpRes
import cn.wecloud.im.multiplayervideodemo.http.model.LiveMeetRes
import cn.wecloud.im.multiplayervideodemo.http.model.MultiMeetSignReq
import cn.wecloud.im.multiplayervideodemo.http.model.SignReq
import io.reactivex.Observable

class HttpManager {

    fun getSign(
        clientId: String, appKey: String, appSecret: String, timestamp: String, platform: Int
    ): Observable<String> {
        return RetrofitManager.commonService
            .getSign(SignReq(clientId, appKey, appSecret, timestamp, platform))
    }

    /**
     * 获取多人音视频专用sign
     */
    fun getMultiMeetSign(
        timestamp: String, clientId: String, appKey: String, platform: Int, appSecret: String,
    ): Observable<BaseHttpRes<String>> {
        return RetrofitManager.commonService
            .getMultiMeetSign(
                MultiMeetSignReq(timestamp, clientId, appKey, platform, appSecret)
            )
            .map {
                if (it.code != 200) {
                    throw HttpException(it.code, it.message)
                }
                return@map it
            }
    }

    /**
     * 创建视频会议
     */
    fun createMeetLive(
        clientId: String, appKey: String, sign: String, platform: Int, timestamp: String,
    ): Observable<BaseHttpRes<LiveMeetRes>> {
        return RetrofitManager.liveMeetService
            .createMeetLive(clientId, appKey, sign, platform, timestamp)
            .map {
                if (it.code != 200) {
                    throw HttpException(it.code, it.message)
                }
                return@map it
            }
    }

    /**
     * 加入视频会议
     */
    fun joinMeetLive(
        roomId: String,
        clientId: String,
        appKey: String,
        sign: String,
        platform: Int,
        timestamp: String,
    ): Observable<BaseHttpRes<LiveMeetRes>> {
        return RetrofitManager.liveMeetService
            .joinMeetLive(roomId, clientId, appKey, sign, platform, timestamp)
            .map {
                if (it.code != 200) {
                    throw HttpException(it.code, it.message)
                }
                return@map it
            }
    }

}